package com.letter.otools.day.util;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.util.Base64;
import android.widget.ImageView;

import com.bumptech.glide.Glide;

/**
 * @Description: java类作用描述
 * @Author: dinghu
 * @CreateDate: 2022/5/31 10:11
 */
public class ImageUtils {
    public static Bitmap base64ToBitmap(String strBase64) {
        strBase64 = strBase64.replaceAll("data:image/jpg;base64,", "");
        byte[] decodedString = Base64.decode(strBase64, Base64.DEFAULT);
        Bitmap decodedByte = BitmapFactory.decodeByteArray(decodedString, 0, decodedString.length);
        return decodedByte;
    }

    public static void showImage(Context context, String imageUrl, ImageView imageView) {
        if (StringUtils.isEmpty(imageUrl)) {
            return;
        }

        Glide.with(context).load(imageUrl).into(imageView);

    }

    public static boolean isImage(String filePath) {
        if (!filePath.contains(".")) {
            return true;
        }

        String extension = filePath.substring(filePath.lastIndexOf(".") + 1);

        // 常见的图片文件扩展名
        String[] imageExtensions = new String[]{"jpg", "jpeg", "png", "gif", "bmp", "webp"};

        for (String imgExtension : imageExtensions) {
            if (imgExtension.equalsIgnoreCase(extension)) {
                return true;
            }
        }

        return false;
    }


    public static boolean isSureImage(String filePath) {
        if (!filePath.contains(".")) {
            return false;
        }

        String extension = filePath.substring(filePath.lastIndexOf(".") + 1);

        // 常见的图片文件扩展名
        String[] imageExtensions = new String[]{"jpg", "jpeg", "png", "bmp"};

        for (String imgExtension : imageExtensions) {
            if (imgExtension.equalsIgnoreCase(extension)) {
                return true;
            }
        }

        return false;
    }

}
